package cn.gary.oaserver.config.security;

import cn.gary.oaserver.entity.TAdmin;
import cn.gary.oaserver.service.ITAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @Author: 邓必宏
 * @title:
 * @Demo:
 * @DateTime: 2024/3/15
 */
@Service("userDetailsService")
public class UserDetailImpl implements UserDetailsService {

    @Autowired
    PasswordEncoder passwordEncoder;

    @Autowired
    ITAdminService adminService;




    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        TAdmin admin = adminService.getAdminByUsername ( username );
        if (null != admin){
            admin.setRoles ( adminService.findRolesAndMenusByAdminId ( admin.getId () ) );
            return admin;
        }
        throw new UsernameNotFoundException ( "用户或者密码不正确" );
    }
};
